package com.ssy.wireless.api.service;

import com.ssy.wireless.entity.ApDevice;
import com.ssy.wireless.entity.ApDeviceRecover;
import com.ssy.wireless.entity.LocalResource;
import com.ssy.wireless.mapper.ApDeviceMapper;
import com.ssy.wireless.mapper.ApDeviceRecoverMapper;
import com.ssy.wireless.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by cofey on 16/11/22.
 */
@Service
@Transactional(readOnly = true)
public class ApiApDeviceRecoverService {

    @Autowired
    private ApDeviceMapper apDeviceMapper;

    @Autowired
    private ApDeviceRecoverMapper apDeviceRecoverMapper;

    @Transactional
    public void insert(ApDeviceRecover apDeviceRecover) {
        Assert.notNull(apDeviceRecover, "设备故障维修记录对象不能为空");
        Assert.isTrue(StringUtils.hasLength(apDeviceRecover.getApIndex()), "AP索引不能为空");
        ApDevice apDevice = apDeviceMapper.selectByApIndex(apDeviceRecover.getApIndex());
        Assert.notNull(apDevice, "当前AP索引[" + apDeviceRecover.getApIndex() + "]不存在");

        Long faultId = apDeviceRecoverMapper.selectLastFaultTime(apDevice.getId());
        Assert.notNull(faultId, "当前故障未发生过故障,无需维修记录");
        ApDeviceRecover recover = new ApDeviceRecover();
        recover.setEntId(apDevice.getEntId());
        recover.setHotspotId(apDevice.getHotspotId());
        recover.setApDeviceId(apDevice.getId());
        recover.setFaultId(faultId);
        recover.setRecoverTime(null == apDeviceRecover.getRecoverTime() ? DateUtils.getTime() : apDeviceRecover.getRecoverTime());
        apDeviceRecoverMapper.insert(recover);

        apDeviceMapper.updateRunStatus(1, apDevice.getId(), apDevice.getHotspotId());
    }
}
